using namespace std;
#include<iostream>
#include<vector>
#include<string>
#include<fstream>
#include<algorithm>
#include<map>
#include<set>
#include"BA2.h"

std::vector<string> cmdList;
std::vector<Indiv> sample;
std::map<string,int> popToindex;
std::map<int,string> indexTopop;
std::map<string,int> labelToindex;
std::map<int,string> indexTolabel;
std::set<string> labelSet;
std::set<string> popSet;
std::vector< vector< vector<long int> > > alleleCounts;
std::vector< set<string> > alleleSet;
std::vector< map<string,int> > alleleMap;
std::vector< map<int,string> > intToallele;

int main()
{
  std::vector< vector <vector <double> > > alleleFreqs;
  std::vector<double> popFstats;
  std::vector< vector <double> > migrationRates;
  std::vector<string> populationNames;
  std::string filename="junk";
  std::string ofilename="BA2.out";  

  ofstream outfile;
  ifstream infile;

  Parameters pvalues;
  string cmd;
  int command;
  int endBA=0;
  bool infileOpen=0;
  enum { quit=1, badcommand=0, scan=2, pset=3, help=4, printData=5, printAlleles=6 };

  PrintBanner();
  string t1;
  while(!endBA)
    {
      cout << "BA>";
      getline(cin,cmd);
      while(cmd.find(";")==string::npos)
	{
	  cout << "/";
	  getline(cin,t1);
	  cmd=cmd+t1;
	}
      int pos=cmd.find_first_of(";");
      cmd=cmd.substr(0,pos);
      command=TranslateCommand(cmd);
      switch(command)
	{
	case scan:
	  if(cmdList.size()>2)
	    {
	      infile.close();
	      infile.open(cmdList[2].c_str());
	      pvalues.fileFormat=cmdList[1];	    
	      if(infile)
		{
		  ReadData(infile,pvalues);
		  infileOpen=1;
		}
	      else
		cout << "Failed to open file: " << cmdList[2] << "\n";
	    }
	  else
	    {
	      cout << "Missing argument for scan!\n";
	    }
	  break;
	case quit:
	  endBA=1;
	  cout << "exiting BA2...\n";
	  break;
	case badcommand:
	  cout << cmd << ": command not found\n";
	  break;
	case pset:
	  if(SetParameters(pvalues)==1)
	    {
	      outfile.close();
	      outfile.open(pvalues.outfile.c_str());
	    }
	  break;
	case help:
	  PrintCommands();
	  break;
	case printData:
	  if(infileOpen)
	    PrintData();
	  else
	    cout << "Cannot execute printData (no input file).\n";
	  break;
	case printAlleles:
	  if(infileOpen)
	    PrintAlleleCounts();
	  else
	    cout << "Cannot execute printAlleles (no input file).\n";
	  break;
	}
    }
} 
